<?
//引入所有子系统信息
include_once("../../../common/script/extender_import.php");


//**获取当前板块资料，必然载入，后续很多读取板块数据库的表格，都写成动态的了。



//获取板块的设置
include_once("../model/setting.php");




//根据页面数量信息，筛选信息，创造url，供页码,跳转等使用
include_once("script/set_url.php");



    function extractLinks($html) {
        // 初始化结果数组
        $result = [];

        // 使用正则表达式匹配所有 <a> 标签
        $pattern = '/<a\s+[^>]*href=["\']([^"\']+)["\'][^>]*>(.*?)<\/a>/i';
        if (preg_match_all($pattern, $html, $matches, PREG_SET_ORDER)) {
            foreach ($matches as $match) {
                // $match[1] 是 href 属性的值
                // $match[2] 是 <a> 标签内的内容
                $result[] = [
                    'href' => $match[1],
                    'label' => $match[2]
                ];
            }
        }

        return $result;
    }





//读取文件内容，赋值到变量

    $filePath=ROOT_PATH_."/uploadfiles/sourcefile/".$_REQUEST['name'];


    $_data = file_get_contents($filePath);

    if ($_data === false) {
        $error = error_get_last();
        echo "无法读取文件: " . $filePath . "\n";
        echo "错误信息: " . ($error ? $error['message'] : "未知错误");
    }


    $_data=cleanhtml($_data,'<a>');


    //去掉所有的class
    $_data = preg_replace('/\sclass="[^"]*"/', '', $_data);


    $_data=extractLinks($_data);


    foreach ($_data as $key => $value)
    {
             $_data_ai[$key]['ai_url']=$value['href'];
             //去掉字符串?__tn__=%3C
             $_data_ai[$key]['ai_url']=str_replace("?__tn__=%3C","",$_data_ai[$key]['ai_url']);


             $_data_ai[$key]['ai_title']=htmlspecialchars_decode(htmlspecialchars($value['label']));
             //替换掉单引号
             $_data_ai[$key]['ai_title']=str_replace("'","",$_data_ai[$key]['ai_title']);

    }

    foreach ($_data_ai as $key => $value)
    {
        $_sql=insert_data('athlon_facebook_page_url',$value);
        //执行语句
        $query = mysql_query($_sql) or die ("error:".mysql_error());
    }




    //数据库，清除url重复的数据，
    $_sql="DELETE a
FROM athlon_facebook_page_url a
JOIN (
    SELECT url, MIN(nid) AS min_id
    FROM athlon_facebook_page_url
    GROUP BY url
    HAVING COUNT(*) > 1
) b ON a.url = b.url AND a.nid > b.min_id";
    $query = mysql_query($_sql) or die ("error:".mysql_error());

    //把数组转换成字符串
    $_data=json_encode($_data_ai);



?>

